###################################################################################################
## "replication_otherstudies.R" from replication package for "Registering Returning Citizens to Vote"
## This script shows the creation of several SI tables about other studies
## (comparison-group study, pilots, postcard test)
###################################################################################################
rm(list = ls())
library(stargazer)
library(xtable)
library(tidyverse)

########################################
## comparison-group study:
## SI Tables A2 and A13
########################################

comp <- read_csv("comparison_party_treatment.csv")
NC <- read_csv("NC_party_treatment.csv")

#as in main dataset, scaling outcomes by 100 to suit R1 table prefs
comp$reg2020 <- comp$reg2020*100
comp$voted2020gen <- comp$voted2020gen *100

NC$reg2020 <- NC$reg2020*100
NC$voted2020gen <- NC$voted2020gen *100

#now, remake partisan-registration table with a comparison to main study
comppartyTC <- table(comp$party, comp$Treatment)
mainpartyTC <- table(NC$party, NC$anytreat) #so note I'm including all arms here, not just T1/T3 (could change if we want)
mainpartyTC <- rbind(mainpartyTC, c(sum(mainpartyTC[,1]), sum(mainpartyTC[,2]))) #add totals row (sum up the other rows)
comppartyTC <- rbind(comppartyTC, c(sum(comppartyTC[,1]), sum(comppartyTC[,2])))

#set up xtable
partytable <- xtable(cbind(mainpartyTC[c(1,3,2,4,5),c(2,1)], comppartyTC[c(1,3,2,4,5),c(2,1)]),label = "tab:parties", caption = "Party of Registration for Registrants in Main Study and Comparison Group") #reorder the rows/columns for easier reading
rownames(partytable) <- c("Democratic", "Republican", "Libertarian", "Not Affiliated", "Total new registrants")
colnames(partytable) <- c("Any Treatment", "Control", "Treatment", "Control")

#build header/notes
#old version before trimming down for PNAS
#a_header <- list(list(-1, 5), c("\\hline\\multicolumn{1}{c}{} & \\multicolumn{2}{c}{Criminal Record Sample} & \\multicolumn{2}{c}{Comparison Group}  \\\\  \\cmidrule(lr){2-3} \\cmidrule(lr){4-5} \n  ", 
#	"\\hline  \\multicolumn{5}{L{12.25cm}}{\\scriptsize{\\textbf{Notes: }This table shows the number of people in each sample who registered to vote, by their party of registration.}} \\\\"))

a_header <- list(list(-1), c("\\hline\\multicolumn{1}{c}{} & \\multicolumn{2}{c}{Criminal Record Sample} & \\multicolumn{2}{c}{Comparison Group}  \\\\  \\cmidrule(lr){2-3} \\cmidrule(lr){4-5} \n  "))


#print
print(partytable, file="comparisongroupandmain_partisanregistrationNC_bytreat.tex", #table A2
            size = "footnotesize", add.to.row = a_header,
            hline.after = c(0, 4),
            caption.placement = "top")



## Now, for Table A13

NCT1T3 <- NC[NC$Treatment=="T1" | NC$Treatment=="T3", c("Treatment", "reg2020", "voted2020gen")]
compgrp <- comp[, c("Treatment", "reg2020", "voted2020gen")]

NCT1T3$group <- "mainstudy"
compgrp$group <- "comparisongroup"

combineddata <- rbind(NCT1T3, compgrp); dim(combineddata)

combined_reg_componly1 <-  lm(reg2020 ~ Treatment , data=combineddata[combineddata$group=="comparisongroup",]); summary(combined_reg_componly1)
combined_reg_mainonly1 <-  lm(reg2020 ~ Treatment , data=combineddata[combineddata$group=="mainstudy",]); summary(combined_reg_mainonly1)
combined_reg_both1 <-  lm(reg2020 ~ Treatment + group + Treatment*group, data=combineddata); summary(combined_reg_both1)

combined_vote_componly1 <-  lm(voted2020gen ~ Treatment , data=combineddata[combineddata$group=="comparisongroup",]); summary(combined_reg_componly1)
combined_vote_mainonly1 <-  lm(voted2020gen ~ Treatment , data=combineddata[combineddata$group=="mainstudy",]); summary(combined_reg_mainonly1)
combined_vote_both1 <-  lm(voted2020gen ~ Treatment + group + Treatment*group, data=combineddata); summary(combined_reg_both1)

stargazer(combined_reg_componly1, combined_reg_mainonly1, combined_reg_both1, combined_vote_componly1, combined_vote_mainonly1, combined_vote_both1, 
	out = "mainandcomparisongroup_registrationvotingcomparison.tex", 
	keep = c("Treatment","group"), keep.stat="n", #omit intercept
	covariate.labels=c("Treatment mailer", "Criminal record group", "Treatment * Record"), 
	title="Treatment Effects for Comparison Group Versus Main Study Group",
	label="comparisonturnoutreg", digits=1,
	add.lines = list(c("Control Mean", round(coef(combined_reg_componly1)[1],2),round(coef(combined_reg_mainonly1)[1],2), 
		round(mean(combineddata[combineddata$Treatment=="T1",]$reg2020),2), round(coef(combined_vote_componly1)[1],3),round(coef(combined_vote_mainonly1)[1],2), 
		round(mean(combineddata[combineddata$Treatment=="T1",]$voted2020gen),3))), #add control means since we omit intercept 
	dep.var.labels=c("Voter Registration", "Voter Turnout"), 
	font.size = "footnotesize", column.labels=c("Comparison" ,"Main Study","All", "Comparison" ,"Main Study","All")) #, 
	#notes = "\\parbox[t]{\\textwidth}{NOTES and stuff}",
	#notes.append = FALSE,notes.align = "l")



########################################
## Address-quality (postcard) study
########################################

mainNC <- read_csv("NCpostcards_deid.csv") #produced in replication_preparation.R since it merges identifiable name/address data together

#look in a multivariate way at what predicts bounces
bouncereg <- lm(bounced ~ male + black + age + time_since_release, data=mainNC)
summary(bouncereg)

#make a table
star <- stargazer(bouncereg, 
         # out="../../Figures/postcards_predictingbounces.tex",
	covariate.labels=c("Male", "Black","Age", "Time since Release"), 
	title="Postcard Followup: Predicting Bounced Mailers",# column.labels=c("Black" ,"White","Both"),
	dep.var.labels=c("Bounced"), font.size = "footnotesize",
	label = "tab:postcards",#add.lines = list(c("Control Mean",round(coef(bouncereg)[1], 3))),
	omit.stat=c("f","rsq","adj.rsq","ser"),  
	notes="This will be replaced", notes.align="l", notes.append=F)


note.latex <- "\\multicolumn{2}{l} {\\parbox[t]{11cm}{ \\textit{Notes:} This table shows the relationship between individual characteristics and whether a mailer ``bounced” (was returned to sender) – a proxy for a wrong address. Specifically, it shows the results of a regression with ``mailer bounced” on the left-hand side, and individual characteristics on the right-hand side. $^{*}$p$<$0.1; $^{**}$p$<$0.05; $^{***}$p$<$0.01}} \\\\"
star[grepl("Note",star)] <- note.latex
cat (star, sep = "\n")

write(star, "postcards_predictingbounces.tex")

########################################
## pilot studies
########################################
rm(list = ls())
NC <- read_csv("NCpilot_deid.csv") #produced in "replication_preparation.R"

#spring 2025: multiply outcome measures by 100 to scale point ests as R1 requires
NC$reg1monthout <- NC$reg1monthout*100
NC$voted2020prim <- NC$voted2020prim*100
NC$voted2020gen <- NC$voted2020gen*100

############################################################################################
## Table A11
############################################################################################


allpilots1m <-lm(reg1monthout ~ treat +as.factor(pilotnum), data=NC)
summary(allpilots1m)

pilot1_1m <-lm(reg1monthout ~ treat, data=NC[NC$pilotnum==1,])
summary(pilot1_1m)

pilot2_1m <-lm(reg1monthout ~ treat, data=NC[NC$pilotnum==2,])
summary(pilot2_1m)

pilot3_1m <-lm(reg1monthout ~ treat, data=NC[NC$pilotnum==3,])
summary(pilot3_1m)


# top panel
registrationpanel2trim <- stargazer(pilot1_1m, pilot2_1m, pilot3_1m, allpilots1m, #out="../Figures/piloteffects_registration_longshortterm.tex", dep.var.caption  = "\\emph{Dependent Variable: Registered to Vote}",
	font.size = "scriptsize", keep.stat="n",  label="pilotsshortterm",
	covariate.labels=c("Treatment (Any mailer)"), keep="treat",digits=1,
	#dep.var.labels = c("Reg (30-day)", "Reg (Nov.)", "Reg (30-day)", "Reg (Nov.)", "Reg (30-day)", "Reg (Nov.)", "Reg (30-day)", "Reg (Nov.)"), 
	add.lines = list(c("Study Fixed Effects", "", "", "","X"), 
	c("Control Group Mean", round(coef(pilot1_1m)[1],2), round(coef(pilot2_1m)[1],2), round(coef(pilot3_1m)[1],2),   round(coef(allpilots1m)[1],2))),
	dep.var.labels = c("Registration"),
	#dep.var.labels = c("Registration", "Registration", "Registration", "Registration"), 
	column.labels   = c("Pilot 1", "Pilot 2", "Pilot 3", "All"), #column.separate = c(2, 2, 2, 2), #, #title="Studies 1-3: Effects on Voter Registration (Any Mailer vs. Control)"
 	title="Studies 1-3: Effects on Voter Registration"
	)


allpilots1m_sep <-lm(reg1monthout ~ treatmentarm +as.factor(pilotnum), data=NC)
summary(allpilots1m_sep)

pilot1_1m_sep <-lm(reg1monthout ~ treatmentarm, data=NC[NC$pilotnum==1,])
summary(pilot1_1m_sep)

pilot2_1m_sep <-lm(reg1monthout ~ treatmentarm, data=NC[NC$pilotnum==2,])
summary(pilot2_1m_sep)

pilot3_1m_sep <-lm(reg1monthout ~ treatmentarm, data=NC[NC$pilotnum==3,])
summary(pilot3_1m_sep)


# bottom panel
registrationpanel1trim <- stargazer(pilot1_1m_sep, pilot2_1m_sep, pilot3_1m_sep, allpilots1m_sep, 
	#out="../Figures/piloteffects_registration_longshortterm_separatearms.tex", 
	font.size = "scriptsize", label="pilotsshortterm",digits=1,
	keep.stat="n", 	omit="pilotnum",          dep.var.caption  = "\\emph{Dependent Variable: Registered to Vote}",
	keep = c("treatment"), #remove this to put the intercept back in.
	#covariate.labels=c("Basic Mailer (Pilot 1)", "Basic Mailer (Pilot 2)", "Basic Mailer + Text (Pilot 2)", "Basic Mailer (Pilot 3)", "Branded Mailer (Pilot 3)", "Branded Mailer + Call (Pilot 3)"), 
	covariate.labels=c("Basic Mailer", "Basic Mailer + Text", "Branded Mailer", "Branded Mailer + Call"), 
	add.lines = list(c("Study Fixed Effects", "", "", "","X"), 
	c("Control Group Mean", round(coef(pilot1_1m)[1],2),round(coef(pilot2_1m)[1],2),   round(coef(pilot3_1m)[1],2),  round(coef(allpilots1m)[1],2))),
	dep.var.labels = c("Registration"),
#	dep.var.labels = c("30 days", "November", "30 days", "November", "30 days", "November","30 days", "November"), 
	column.labels   = c("Study 1", "Study 2", "Study 3", "All")) # column.separate = c(2, 2, 2, 2), title="Studies 1-3: Short- and Longer-Term Effects on Voter Registration") 

#stick those two panels together:
twopanelpilotregtrim <- star_panel(registrationpanel2trim, registrationpanel1trim, 
	panel.names= c("All Arms Combined", "Separate Treatment Arms"), 
	panel.label.fontface = "bold") #%>%
#	star_notes_tex(note.type="threeparttable", note="This table shows the effect of each treatment (relative to the control), as well as pooled treatment arms relative to control,  on voter registration.  $^{*}$p$<$0.1; $^{**}$p$<$0.05; $^{***}$p$<$0.01")

star_tex_write(twopanelpilotregtrim, file ="piloteffects_registration_stacked_separatearms.tex", headers = FALSE)


############################################################################################
## Table A12
############################################################################################
#and we also want to look at turnout in the march primary (for pilot 1)
pilot1_primturnout <-lm(voted2020prim ~ treat, data=NC[NC$pilotnum==1,])
summary(pilot1_primturnout)

allpilots_turnout <-lm(voted2020gen ~ treat +as.factor(pilotnum), data=NC)
summary(allpilots_turnout)

pilot1_turnout <-lm(voted2020gen ~ treat, data=NC[NC$pilotnum==1,])
summary(pilot1_turnout)

pilot2_turnout <-lm(voted2020gen ~ treat, data=NC[NC$pilotnum==2,])
summary(pilot2_turnout)

pilot3_turnout <-lm(voted2020gen ~ treat, data=NC[NC$pilotnum==3,])
summary(pilot3_turnout)


pilot1_turnout_sep <-lm(voted2020gen ~ treatmentarm, data=NC[NC$pilotnum==1,])
summary(pilot1_turnout_sep)
pilot2_turnout_sep <-lm(voted2020gen ~treatmentarm, data=NC[NC$pilotnum==2,])
summary(pilot2_turnout_sep)
pilot3_turnout_sep <-lm(voted2020gen ~ treatmentarm, data=NC[NC$pilotnum==3,])
summary(pilot3_turnout_sep)

allpilots_turnout_sep <-lm(voted2020gen ~ treatmentarm +as.factor(pilotnum), data=NC)
summary(allpilots_turnout_sep)

pilot1_primturnout_sep <-lm(voted2020prim ~ treatmentarm, data=NC[NC$pilotnum==1,])
summary(pilot1_primturnout)

#no longer saving this as standalone table: making it a panel in a two-parter.
#stargazer(pilot1_primturnout,pilot1_turnout, pilot2_turnout, pilot3_turnout, allpilots_turnout, 
#	out="../Figures/piloteffects_turnout_post2020general_incprimary_separatearms.tex", 
#	title="Studies 1-3: Effects on Voter Turnout", 
#	covariate.labels=c("Basic Mailer", "Basic Mailer + Text", "Branded Mailer", "Branded Mailer + Call"), 
#	label="pilotturnout",
#	keep.stat="n", column.labels=c("Study 1", "Study 1", "Study 2", "Study 3", "All Studies"), omit="pilotnum",
#	add.lines = list(c("Study fixed effects", "", "", "", "", "X"), 
#	c("Control Group Mean", round(coef(pilot1_primturnout)[1],3),round(coef(pilot1_turnout)[1],3),round(coef(pilot2_turnout)[1],3),round(coef(pilot3_turnout)[1],3),round(coef(allpilots_turnout)[1],3))),
#	keep="treatment", #comment this to put intercept back in.
#	dep.var.labels=c("Voted in March 2020", "Voted in November 2020"), font.size = "footnotesize")

turnoutpanel1 <- stargazer(pilot1_primturnout_sep, pilot1_turnout_sep, pilot2_turnout_sep, pilot3_turnout_sep, allpilots_turnout_sep,  
	#out="../Figures/piloteffects_turnout_post2020general_incprimary_separatearms.tex", 
	#title="Studies 1-3: Effects on Voter Turnout", 
	covariate.labels=c("Basic Mailer", "Basic Mailer + Text", "Branded Mailer", "Branded Mailer + Call"), 
	label="pilotturnout",digits=1,
	keep.stat="n", column.labels=c("Study 1", "Study 1", "Study 2", "Study 3", "All Studies"), omit="pilotnum",
	add.lines = list(c("Study fixed effects", "", "", "", "", "X"), 
	c("Control Group Mean", round(coef(pilot1_primturnout)[1],2),round(coef(pilot1_turnout)[1],2),round(coef(pilot2_turnout)[1],3),round(coef(pilot3_turnout)[1],2),round(coef(allpilots_turnout)[1],2))),
	keep="treatment", #comment this to put intercept back in.
	dep.var.labels=c("Voted in March 2020", "Voted in November 2020"), font.size = "footnotesize")


turnoutpanel2 <- stargazer(pilot1_primturnout, pilot1_turnout, pilot2_turnout, pilot3_turnout, allpilots_turnout, 
	#out="../Figures/piloteffects_turnout_post2020general_incprimary.tex", 	
	title="Studies 1-3: Effects on Voter Turnout", 
	covariate.labels=c("Treatment (Any mailer)"), 
	label="pilotturnout",digits=1,
	keep.stat="n", column.labels=c("Study 1", "Study 1", "Study 2", "Study 3", "All Studies"), omit="pilotnum",
	add.lines = list(c("Study fixed effects", "", "", "", "", "X"), 
	c("Control Group Mean", round(coef(pilot1_primturnout)[1],2),round(coef(pilot1_turnout)[1],2),round(coef(pilot2_turnout)[1],2),round(coef(pilot3_turnout)[1],2),round(coef(allpilots_turnout)[1],2))),
	keep="treat", #comment this to put intercept back in.
	dep.var.labels=c("Voted in March 2020", "Voted in November 2020"), font.size = "footnotesize")


#stick those two panels together and make the paneled table:
twopanelpilotturnout <- star_panel(turnoutpanel2, turnoutpanel1, 
	#panel.names= c("Separate Treatment Arms", "All Arms Combined"), 
	panel.names= c( "All Arms Combined", "Separate Treatment Arms"), 
	panel.label.fontface = "bold") %>%
	star_notes_tex( note.type="threeparttable", note="This table shows the effect of each treatment (relative to the control), as well as pooled treatment arms relative to control,  on voter turnout. $^{*}$p$<$0.1; $^{**}$p$<$0.05; $^{***}$p$<$0.01")

star_tex_write(twopanelpilotturnout, file ="piloteffects_turnout_post2020general_incprimary_separatearms.tex", headers = FALSE)
